Ext.define('AmpleHarvest.objects.metadata.cMetaData',
{
	extend			: 'Ext.grid.Panel',
	alias			: 'widget.ampleharvest-objects-metadata-cmetadata',
	title			: 'Meta Data',
	collapsible		: true,
	iconCls			: 'toolbar-article-enhancement-metadata',
	border			: 0,
	requires		:
	[
			"Ext.data.Store", 'AmpleHarvest.DATA.Model.cMetadata'
	],
	columns			:
	[
			{
				header		: "Name",
				dataIndex	: "name",
				flex		: 1
			},
			{
				header		: "Value",
				dataIndex	: "value",
				flex		: 1
			},
			{
				xtype	: 'actioncolumn',
				width	: 25,
				items	:
				[
						{
							iconCls	: 'toolbar-article-navigation-pivot action-icon',
							tooltip	: 'Pivot',
							handler	: function(obj_Grid, int_RowIndex,
									int_ColIndex)
							{
								var obj_Rec = obj_Grid.getStore().getAt(int_RowIndex);
								var str_Prop = "string."+obj_Rec.get("name");
								var str_Value = obj_Rec.get("value");

								str_Prop = str_Prop.toLowerCase();
								str_Prop = str_Prop.replace(/ /gi,"_");
								
								if (!str_Value)
								{
									Ext.Msg.alert("Error",
											"You must enter some data to search for."
									);
									return;
								}
								
								var obj_Query = new Array();
								obj_Query.push(
										{
											property	: str_Prop,
											value		: str_Value,
											type		: "condition",
											condition	: '==',
											join		: 'AND'
										});
								m_obj_AmpleHarvest_ViewPort
										.fireEvent(
												AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_ADVANCED_QUERY,
												obj_Query
										);
							}
						}
				]
			}
	],
	bubbleEvents	:
		[
			'doclassificationchange'
		],
	listeners		:
	{
		brokerResponse	: function(obj_response)
		{
			this.add_JsonObject(obj_response.data);
			this.ahCaller.fireEvent('objectResponse', this);
			this.fireEvent(
					AmpleHarvest.layout.cLayout.C_STR_EVENT_CLASSIFICATION_CHANGE,
					obj_response.classification
			);
		}
	},
	store			: Ext.create('Ext.data.Store',
			{
				model	: 'AmpleHarvest.DATA.Model.cMetadata',
				proxy	:
				{
					type	: 'memory'

				}

			}),

	initComponent	: function()
	{
		this.addEvents('brokerResponse');
		
		var obj_This = this;
		
		var util_db_dmetedata = Ext.create('AmpleHarvest.util.dataBroker');
		util_db_dmetedata.brokerData(this, this.m_str_id, 'metadata');	

		this.callParent();
	},

	add_JsonObject	: function(JsonObject)
	{
		var metaData = [];
		this.itemCount = 0;
		for (var value in JsonObject)
		{

			this.store.add(AmpleHarvest.DATA.Model.cMetadata.create(
					{
						name	: JsonObject[value].name,
						value	: JsonObject[value].value
					}));
			this.itemCount++;
		}
	},

	hasData			: function()
	{
		if (!this.itemCount)
		{
			return false;
		}
		if (this.itemCount < 1)
		{
			return false;
		}
		this.setTitle(this.title + ' (' + this.itemCount + ')');
		return true;
	}
}
);
